﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.UnifyShow.Business;
using Microsoft.Practices.Unity;

namespace Wicresoft.UnifyShow.Portal
{
    public class HomeController : BaseController
    {
        private IUser_X user;
        private ISiteBranch siteBranch;
        private ISiteBranchAndDeviceAndAlert siteBranchAndDeviceAndAlert;
        private IBusinessCategory businessCategory;
        private IConfiguration configuration;
        private IIncident incident;
        private IChange change;
        private IProblem problem;

        [InjectionConstructor]
        public HomeController(IUser_X user, ISiteBranch isiteBranch, ISiteBranchAndDeviceAndAlert isiteBranchAndDeviceAndAlert, IBusinessCategory ibusinessCategory, IIncident iincident, IChange ichange, IConfiguration iconfiguration,IProblem problem)
        {
            this.user = user;
            siteBranch = isiteBranch;
            siteBranchAndDeviceAndAlert = isiteBranchAndDeviceAndAlert;
            businessCategory = ibusinessCategory;
            incident = iincident;
            change = ichange;
            configuration = iconfiguration;
            this.problem = problem;

        }
        protected override void Dispose(bool disposing)
        {
            ((IDisposable)siteBranch).Dispose();
            ((IDisposable)siteBranchAndDeviceAndAlert).Dispose();
            ((IDisposable)businessCategory).Dispose();
            ((IDisposable)incident).Dispose();
            ((IDisposable)change).Dispose();
            ((IDisposable)configuration).Dispose();
            ((IDisposable)user).Dispose();
            ((IDisposable)problem).Dispose();
        }

        //
        // GET: /Home/

        public ActionResult Index()
        {
            base.SetRoles(user);
            int userID = (int)UserID;
            if (userID < 0 || UserSiteIDs.Count == 0) return new HttpStatusCodeResult(403);
            List<SiteBranchModel> model = siteBranch.GetSiteBranchByUserID(userID).OrderBy(o => o.ID).ToList();
            return View(model);
        }

        public ActionResult BusinessSystemInfo(int siteID)
        {
            base.SetRoles(user);
            int userID = (int)UserID;
            List<BusinessCategoryAndSystemModel> model = new List<BusinessCategoryAndSystemModel>();
            if (configuration.GetValueByKey("homepage_system_sort_desc") == "1")
            {
                model = businessCategory.GetBusinessCategoryAndSystem(siteID, userID, true);
            }
            else
            {
                model = businessCategory.GetBusinessCategoryAndSystem(siteID, userID, false);
            }
            return PartialView(model);
        }

        public ActionResult HomeContent(int siteID)
        {
            base.SetRoles(user);
            int userID = (int)UserID;
            if (!UserSiteIDs.Contains(decimal.Parse(siteID.ToString()))) return new HttpStatusCodeResult(403);

            IList<DeviceAndAlertCountModel> alertCount = siteBranch.GetSiteAlertCountBySiteID(siteID);

            List<SiteBranchAndWorkListModel> incidentList = incident.GetIncidentInfoList(siteID, null, null, null);
            List<SiteBranchAndWorkListModel> changeList = change.GetChangeInfoList(siteID, null, null, null);
            List<SiteBranchAndWorkListModel> problemList = problem.GetProblemInfoList(siteID, null, null, null);

            ViewData["clickSiteId"] = siteID;
            ViewData["SitesHome"] = UsersSites.Where(o=>o.SiteID==siteID).ToList();
            ViewData["AlertCount"] = alertCount;
            ViewData["SiteBranchAndWorkList"] = incidentList;
            ViewData["SiteBranchAndWorkListBychange"] = changeList;
            ViewData["SiteBranchAndWorkListByproblem"] = problemList;

            return PartialView();
        }

    }
}
